package org.budo.redis.jdbc.driver.statement.mysql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.budo.jdbc.driver.AbstractBudoJdbcPreparedStatement;
import org.budo.jdbc.driver.resultset.impl.MapArrayResultSet;
import org.budo.redis.jdbc.driver.statement.AbstractRedisStatement;
import org.budo.support.lang.util.MapUtil;

/**
 * help 是一个虚拟表，展示所有用法 SELECT * FROM help
 * 
 * @author lmw
 * @see org.budo.redis.jdbc.driver.statement.mysql.SHOW_TABLE_STATUS
 */
public class SELECT_HELP extends AbstractRedisStatement {
    public static final String TABLE_NAME = "sql-redis-help";

    private static final String[][] HELP_LINES = { { "hello you, this is the sql on redis, now you can use your redis like this", "" }, //
            { "", "" }, //
            { "SELECT KEYS('*key-1*')", "SELECT key FROM redis WHERE key LIKE '*key-1*'" }, //
            { "SELECT GET('key-2')", "SELECT value FROM redis WHERE key = 'key-2'" }, //
            { "SELECT SET('key-3', 'value-3')", "UPDATE redis SET value = 'value-3' WHERE key = 'key-3'" }, //
            { "SELECT EXPIRE('key-4', 444)", "UPDATE redis SET expire = 444 WHERE key = 'key-4'" }, //
            { "SELECT DEL('key-5')", "DELETE FROM redis WHERE key='key-5'" }, //
            { "SELECT HGETALL('key-6')", "" }, //
            { "SELECT TYPE('key-7')", "" } };

    @Override
    public void execute(PreparedStatement preparedStatement) throws SQLException{
        String c1 = "c1";
        String c2 = "c2";

        Map[] mapArray = new Map[HELP_LINES.length];
        for (int i = 0; i < HELP_LINES.length; i++) {
            mapArray[i] = MapUtil.stringMap(c1, HELP_LINES[i][0], //
                    c2, HELP_LINES[i][1]);
        }
        
        ResultSet resultSet = new MapArrayResultSet(new String[] { c1, c2 }, mapArray);
        ((AbstractBudoJdbcPreparedStatement) preparedStatement).setResultSet(resultSet);
    }
}
